import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue'; // ？
import path from 'path'; // 配置别名有用到
import viteCompression from 'vite-plugin-compression'; // 打包

// https://vitejs.dev/config/
// export default defineConfig({});

export default ({ mode }) => {
  // eslint-disable-next-line no-console
  console.log('mode', loadEnv(mode, process.cwd())); //127.0.0.1:9000/api
  return defineConfig({
    //这里进行配置别名
    resolve: {
      alias: {
        '@': path.resolve('./src'), // @代替src
        '#': path.resolve('./types'), // #代替types
      },
    },
    // 添加全局样式
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/assets/styles/index.scss";',
        },
      },
    },
    // 配置端口与代理
    server: {
      host: '0.0.0.0',
      port: 8080,
      open: true,
      https: false,
      proxy: {
        '/api': {
          target: 'https://mp.weixin.qq.com',
          changeOrigin: true,
          ws: true,
          rewrite: (path: string) => path.replace(/^\/api/, ''),
        },
      },
    },
    // 打包配置
    build: {
      rollupOptions: {
        // output: { // 打包后及压缩
        //   manualChunks: {
        //     vue: ['vue', 'vue-router'],
        //     // elementIcons: ['@element-plus/icons-vue'],
        //   },
        // },
        output: {
          // js和css文件夹分离
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
        },
      },
    },
    // 插件的使用
    plugins: [
      vue(),
      //...
      {
        ...viteCompression(),
        apply: 'build',
      },
    ],
  });
};
